package com.tjsinfo.tjsoa.message;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import com.tjsinfo.tjsoa.system.vo.User;
import net.sf.jml.Email;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnProtocol;
import net.sf.jml.MsnUserStatus;
import net.sf.jml.event.MsnAdapter;
import net.sf.jml.event.MsnContactListAdapter;
import net.sf.jml.impl.MsnMessengerFactory;


public class JML extends MsnAdapter {
	private MsnMessenger messenger = null;
	private String email = null, password = null;
	public void sendMessageHashtable(final Hashtable<String, String> hashtable,final Hashtable<String, String> feixintable) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Set<Map.Entry<String, String>> enterySet = hashtable.entrySet();
		for (Map.Entry<String, String> ent : enterySet) {
			String value = User.getSendMessage().get(ent.getKey());
			if (value==null) {
				User.getSendMessage().put(ent.getKey(), "ＯＡ系统提示信息：\r\n"+sdf.format(new Date())+" "+ent.getValue());
			}else{
				User.getSendMessage().put(ent.getKey(), value+"\r\n"+sdf.format(new Date())+" "+ent.getValue());
			}
		}
//		Set<Map.Entry<String, String>> enterySet1 = feixintable.entrySet();
//		for (Map.Entry<String, String> ent : enterySet1) {
//			String value = User.getSendMessage().get(ent.getKey());
//			if (value==null) {
//				User.getFeiXin().put(ent.getKey(), "ＯＡ系统提示信息：\r\n"+sdf.format(new Date())+" "+ent.getValue());
//			}else{
//				User.getFeiXin().put(ent.getKey(), value+"\r\n"+sdf.format(new Date())+" "+ent.getValue());
//			}
//		}
	}
	public void sendMessageString(final String userMSN,final String value) {
		messenger = MsnMessengerFactory.createMsnMessenger(email, password);
		messenger.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP12 });
		messenger.getOwner().setInitStatus(MsnUserStatus.ONLINE);
		messenger.addListener(this);
		messenger.login();
		
		
		try {
			messenger.addContactListListener(new MsnContactListAdapter() {

			    public void contactListInitCompleted(MsnMessenger messenger) {
			    	
					messenger.sendText(Email.parseStr(userMSN), value);
					
			    }
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public MsnMessenger getMessenger() {
		return messenger;
	}

	public void setMessenger(MsnMessenger messenger) {
		this.messenger = messenger;
	}
}
